home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / appkit.742 < prev    next >
Text File  |  1992-02-06  |  2KB  |  93 lines

  1. {\rtf0\ansi{\fonttbl\f1\fnil Times-Roman;\f2\fmodern Ohlfs;\f0\fswiss Helvetica;}
  2. \paperw13040
  3. \paperh10800
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\f1\b0\i0\ul0\fs28 text object linebreaks\
  8. \
  9. Q:  How do I figure out where the linebreaks are in a Text object?   When I use the NeXT methods that return character offsets by lines, I actually get paragraphs instead of lines.\
  10. \
  11. A:  This happens because paragraphs are delimited by hard carriage returns, and lines are not.  If you want to get the 
  12. \b real
  13. \b0  line breaks—the ones that are displayed on the screen—then you have to decode the internal variable 
  14. \b theBreaks
  15. \b0 . Here is a snippet of code that shows you how. \
  16. \
  17.  
  18. \b Warning: 
  19. \b0 Be aware that 
  20. \b theBreaks
  21. \b0  is an internal structure to the Text object and may change in future implementations. Your code may not work in future releases! \
  22. \
  23.  
  24. \f2\fs22 /* This piece of code shows how to get line break information out of\
  25.  * a Text object.\
  26.  *\
  27.  * (C) 1990 MIT Media Lab.  By Simson L. Garfinkel\
  28.  */ \
  29. \
  30. #import "Thing.h"\
  31. #import <stdio.h>\
  32. #import <stdlib.h>\
  33. #import <appkit/ScrollView.h>\
  34. #import <appkit/Text.h>\
  35. \
  36. @implementation Thing\
  37. \
  38. - calc:sender\
  39. \{\
  40.     Text        *text = [myScroller docView];\
  41.     int        line;\
  42.     int        pos;\
  43.     NXBreakArray    *theBreaks=0;\
  44.     int        nbreaks;\
  45.     NXLineDesc    *breaks;\
  46.     int        length = [text textLength];\
  47. \
  48.     object_getInstanceVariable(text, "theBreaks", (void **)&theBreaks);\
  49. \
  50.     nbreaks    = theBreaks->chunk.used/sizeof(NXLineDesc);\
  51.     breaks  = theBreaks->breaks;\
  52. \
  53.     for (pos=0, line=0; line<nbreaks && pos<length; line++) \{\
  54.         int    lineChange;\
  55.         int    endParagraph;\
  56.         int    len,nlen;\
  57.         char    *buf;\
  58. \
  59.         lineChange    = breaks[line] & 0x8000;\
  60.         endParagraph    = breaks[line] & 0x4000;\
  61.         \
  62.         len    = breaks[line] & 0x3fff;\
  63.         buf    = malloc(len+1);\
  64.         \
  65.         nlen = [text getSubstring:buf start:pos length:len];\
  66.         buf[nlen] = '\\000';\
  67.         \
  68.         printf("pos=%d len=%d eP=%d buf=%s\\n",\
  69.                pos, nlen, endParagraph, buf);\
  70.         \
  71.         free(buf);\
  72.         pos    += len;\
  73. \
  74.         if (lineChange!=0) \{\
  75.             /* "if the line change bit is set, the descriptor is\
  76.              * the first field of a NXHeightChange...."\
  77.              */\
  78.             line    +=  sizeof(NXHeightInfo) / sizeof(NXLineDesc);\
  79.         \}\
  80.     \}\
  81.     return self;\
  82. \}\
  83. \
  84. @end\
  85. \
  86.  
  87. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f1\fs28\fc0 QA742\
  88. \
  89. Valid for 1.0\
  90. Valid for 2.0\
  91. \
  92.  
  93.